/*
 * Copyright 2010 Joshua Smith and Nelson Llewellyn
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package norse.temp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;
import norse.msg.MessageIF;
import norse.msg.NoRSeScanRequest;
import norse.plugins.nmap.msg.NmapScanRequest;
import norse.util.MessageMarshaller;

/**
 *
 */
public class ServerTest
{
    public static void main(String[] args) throws Exception
    {
        System.out.println("Running");
        
        MessageMarshaller messMarsh = new MessageMarshaller();
        NmapScanRequest nmapScanReq = new NmapScanRequest();
        List<MessageIF> scanReqList = new LinkedList<MessageIF>();
        scanReqList.add(nmapScanReq);
        NoRSeScanRequest rootScanReq = new NoRSeScanRequest(scanReqList);

        String postBody = messMarsh.marshallMessage(nmapScanReq);


        System.out.println("Connecting to URL...");
        URL serverURL = new URL(
                "http://localhost:8080/NoRSeServer/resources/serverMain");
        HttpURLConnection connection
                = (HttpURLConnection)serverURL.openConnection();
        System.out.println("Connection established.");

        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestProperty("Content-Type", "application/xml");

        System.out.println("Sending data...");
        OutputStreamWriter out = new OutputStreamWriter(
                connection.getOutputStream());
        out.write(postBody);
        out.close();
        System.out.println("Data sent.");

        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                connection.getInputStream()));

        String decodedString;

        System.out.println("\nResponse:");
        System.out.println("----------------------------------------------------------------------");
        while ((decodedString = in.readLine()) != null)
        {
            System.out.println(decodedString);
        }
        in.close();
        System.out.println("----------------------------------------------------------------------");
        System.out.println("Program terminating");
    }
}
